#!/bin/bash
# File Name: backup_mongodb.sh
# Author: moshan
# mail: mo_shan@yeah.net
# Created Time: 2019-05-28 15:28:59
# Function: 
#########################################################################
local_ip="$(ip a show dev ens160|grep -w inet|awk '{print $2}'|awk -F '/' '{print $1}')"
backup_dir='/dbarchive/mongodb/109141/'
date_dir=`date +%F`
echo "Start to backup at $(date +%Y%m%d%H%M)"
if [ ! -d "$backup_dir$date_dir" ];then
    mkdir -p $backup_dir$date_dir
fi
/opt/mongodb/bin/mongodump -h127.0.0.1:27017  --authenticationDatabase admin -u root -p Tj6k9z211HwYh17VvQpL  -o $backup_dir$date_dir --gzip
if [ $? -eq 0 ];then
    echo -e "Backup is finish! at $(date +%Y%m%d%H%M)\nServer_name:$(hostname) Server_ip:$local_ip $(date +"%y-%m-%d %H:%M:%S") mysql full backup Success!"| mail -s "[$local_ip]mongodb backup ok" op-db@mljr.com
    exit 0
else
    echo -e "Backup is Fail! at $(date +%Y%m%d%H%M)\nServer_name:$(hostname) Server_ip:$local_ip $(date +"%y-%m-%d %H:%M:%S") mysql full backup Fail!"| mail -s "[$local_ip]mongodb backup fail" op-db@mljr.com
    exit 1
fi
echo "Backup Process Done"

